package com.wuzihao.disk.login.control;

import com.alibaba.fastjson.JSON;
import com.wuzihao.disk.login.entity.MenuLevelEntity;
import com.wuzihao.disk.login.service.LoginService;
import com.wuzihao.disk.mybatis.entity.PtUserEntity;
import com.wuzihao.disk.spring.control.BaseController;
import com.wuzihao.disk.spring.security.UserDiskEntity;
import com.wuzihao.disk.user.exeption.EmailRepeatException;
import com.wuzihao.disk.user.exeption.MobileRepeatException;
import com.wuzihao.disk.user.exeption.UserNameRepeatException;
import com.wuzihao.disk.user.service.UserService;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class MainController extends BaseController {
    @Resource
    private HttpSession httpSession;
    @Resource
    private LoginService loginService;
    @Resource
    private UserService userService;

    @RequestMapping("/")
    public String main(){
        UserDiskEntity userDiskEntity = (UserDiskEntity) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();

        httpSession.setAttribute("userUuidDisk", userDiskEntity.getUserUuid());
        httpSession.setAttribute("lang", LocaleContextHolder.getLocale().toString());
        List<MenuLevelEntity> list = loginService.queryMenuListLevel();
        httpSession.setAttribute("menuList", list);

        return "login/mainPage";
    }

//    @RequestMapping("/queryMenuList")
//    @ResponseBody
//    public String queryMenuList() {
//        List<PtMenuEntity> list = loginService.queryMenuList();
//
//        return JSON.toJSONString(list);
//    }
//
//    @RequestMapping("/queryMenuListLevel")
//    @ResponseBody
//    public String queryMenuListLevel() {
//        List<MenuLevelEntity> list = loginService.queryMenuListLevel();
//
//        return JSON.toJSONString(list);
//    }

    @RequestMapping("/welcomePage")
    public String queryMenuListLevel() {
        return "login/welcomePage";
    }

    @Secured("ROLE_Test1")
    @RequestMapping("/test1")
    public String test1(){
        return "test/test1Page";
    }

    @Secured("ROLE_Test2")
    @RequestMapping("/test2")
    public String test2(){
        return "test/test2Page";
    }

    /**
     * 获取当前登录用户信息
     */
    @RequestMapping("/getCurrentUserInfo")
    @ResponseBody
    public String getCurrentUserInfo() {
        UserDiskEntity userDiskEntity = (UserDiskEntity) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();

        PtUserEntity entity = userService.queryUserInfoById(userDiskEntity.getUserUuid());
        return JSON.toJSONString(entity);
    }

    /**
     * 修改当前用户信息
     */
    @RequestMapping("/modifyCurrentUserInfo")
    @ResponseBody
    public String modifyCurrentUserInfo(PtUserEntity entity) {
        try {
            // 设置当前用户UUID
            UserDiskEntity userDiskEntity = (UserDiskEntity) SecurityContextHolder.getContext()
                    .getAuthentication()
                    .getPrincipal();
            entity.setUserUuid(userDiskEntity.getUserUuid());

            userService.modifyUserInfoById(entity);
        } catch (EmailRepeatException e) {
            return failJson("user.email.repeat");
        } catch (MobileRepeatException e) {
            return failJson("user.mobile.repeat");
        } catch (UserNameRepeatException e) {
            return failJson("user.name.repeat");
        }
        return successJson("user.modify.success");
    }

    /**
     * 修改当前用户密码
     */
    @RequestMapping("/modifyCurrentUserPassword")
    @ResponseBody
    public String modifyCurrentUserPassword(String oldPassword, String newPassword) {
        try {
            // 获取当前用户UUID
            UserDiskEntity userDiskEntity = (UserDiskEntity) SecurityContextHolder.getContext()
                    .getAuthentication()
                    .getPrincipal();

            userService.modifyUserPassword(userDiskEntity.getUserUuid(), oldPassword, newPassword);
        } catch (Exception e) {
            return failJson("password.modify.fail", e.getMessage());
        }
        return successJson("password.modify.success");
    }

}
